package cn.study.controller;


import cn.study.entity.StudyCourse;
import cn.study.entity.StudyVideo;
import cn.study.service.OssService;
import cn.study.service.StudyVideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 视频表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-video")
public class StudyVideoController {
    @Resource
    private StudyVideoService studyVideoService;
    @Resource
    OssService ossService;
    @GetMapping("/list")
    public String list(Model model, @RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize)
    {
        PageHelper.startPage(pn, paginalsize);

        List<StudyVideo> list=studyVideoService.getlist();
        PageInfo pInfo=new PageInfo(list,10);
        model.addAttribute("pInfo",pInfo);
        model.addAttribute("paginalsize",paginalsize);

        return "study_video";
    }
    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyVideoService.removeById(id);
        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-video/list";
    }
    @GetMapping("/add")
    public String add()
    {

        return "study_video_add";
    }

    @PostMapping("/videoadd")
    public String videoadd(StudyVideo studyVideo,@RequestParam("file") MultipartFile file){
        String url=ossService.uploadFileAvatar(file);
        studyVideo.setVideoPath(url);
        studyVideo.setVideopicPath(url);
        boolean bl=studyVideoService.save(studyVideo);


        if(bl){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-video/list";
    }
    @GetMapping("/upd")
    public String upd(Integer id,Model model)
    {
        StudyVideo studyVideo=studyVideoService.getById(id);
        model.addAttribute("list",studyVideo);


        return "study_video_upd";
    }

    @PostMapping("/videoupd")
    public String videoupd(Model model,StudyVideo studyVideo,@RequestParam("file") MultipartFile file)
    { String url=ossService.uploadFileAvatar(file);
        studyVideo.setVideoPath(url);
        studyVideo.setVideopicPath(url);
        studyVideoService.updateById(studyVideo);
        return "redirect:/study-video/list";
    }

}

